#!/usr/bin/env python

import sys
import bsddb
from itertools import groupby

from nw_sim_base import interpolate_learn

def parse_input(lines):
    for line in lines:
        weights_strings, _length, length, ev, sd = line.strip('\n').split('\t')
        yield (weights_strings.split(), int(length), float(ev), float(sd))

def main():
    lines_cnt = 0
    grps_cnt = 0
    for db_keys, grp in groupby(parse_input(sys.stdin), key=lambda i:i[0]):
        grp = list(grp)
        len_sample = [i[1] for i in grp]
        ev_sample  = [i[2] for i in grp]
        sd_sample  = [i[3] for i in grp]

        ev_coefs = interpolate_learn(len_sample, ev_sample)
        sd_coefs = interpolate_learn(len_sample, sd_sample)
        db_value = '%s\t%s' % (
            '|'.join('%g'%c for c in ev_coefs),
            '|'.join('%g'%c for c in sd_coefs),
        )
        for db_key in db_keys:
            print '%s\t%s' % (db_key, db_value)
            lines_cnt += 1
        grps_cnt += 1
    print >> sys.stderr, 'Processed %i groups, %i keys' % (grps_cnt, lines_cnt)

main()

